﻿using HartConfigTool.HARTCommands.CMDs;
using HartConfigTool.SerialPortUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HartConfigTool.UI.DebugWindow
{
    internal sealed class CMDsDebugWinHARTCMD3 : CMD3ReadDynamicVarAndLoopCurrent
    {
        public CMDsDebugWinHARTCMD3(SerialPortsManager serialPortManager) : base(serialPortManager) { }

        protected override async Task SendCMDCode()
        {
            byte[] tempAry = new byte[16];
            byte[] cmdCodeBytes = StructToBytes<CMD3ReadDyncVarsAndLoopCurCodeStruct>(CMD3ReadDyncVarsAndLoopCurCode);

            Array.Copy(cmdCodeBytes, 5, tempAry, 0, 8);
            cmdCodeBytes[cmdCodeBytes.Length - 1] = HARTProtocolV7.GetCRC(tempAry, (byte)8);

            serialPortManager.DebugMessage("[" + DateTime.Now + "]:-->" + HARTProtocolV7.HexToString(cmdCodeBytes) + "\r\n");

            await serialPortManager.SendCMD(cmdCodeBytes);
        }
        public override async Task ParseResponse(byte[] responseData)
        {
            await Task.Run(() =>
            {
                List<string> parts = new List<string>();
                byte[] bytes = null;
                byte[] data = null;

                data = serialPortManager.GetRxBufferData();

                bytes = HARTProtocolV7.GetDataFrame(data);

                // Debug Message
                serialPortManager.DebugMessage("[" + DateTime.Now + "]:<--" + HARTProtocolV7.HexToString(data) + "\r\n");

                // PV Loop Current
                parts.Clear();
                parts.Add(BitConverter.ToSingle(bytes, 10).ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["PV Loop Current"], parts);
                // PV Units Code
                parts.Clear();
                parts.Add(bytes[14].ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["PV Units Code"], parts);
                // PV
                parts.Clear();
                parts.Add(BitConverter.ToSingle(bytes, 15).ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["PV"], parts);
                // SV Units Code
                parts.Clear();
                parts.Add(bytes[19].ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["SV Units Code"], parts);
                // SV
                parts.Clear();
                parts.Add(BitConverter.ToSingle(bytes, 20).ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["SV"], parts);
                // TV Units Code
                parts.Clear();
                parts.Add(bytes[24].ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["TV Units Code"], parts);
                // TV
                parts.Clear();
                parts.Add(BitConverter.ToSingle(bytes, 25).ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["TV"], parts);
                // QV Units Code
                parts.Clear();
                parts.Add(bytes[29].ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["QV Units Code"], parts);
                // QV
                parts.Clear();
                parts.Add(BitConverter.ToSingle(bytes, 30).ToString());
                NotifyObservers(CMD3Observers, ObserversInfo.HARTCMD3Observers["QV"], parts);

                this.serialPortManager.RxDataClear();
            });
        }
    }
}
